package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.OrdersProjectPageParamDTO;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public static final String NURSING_PROJECT_ALL = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int code = nursingProjectMapper.insert(nursingProject);
        clearCache();
        return code;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int code = nursingProjectMapper.updateById(nursingProject);
        clearCache();
        return code;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        return nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int code = nursingProjectMapper.deleteById(id);
        clearCache();
        return code;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        return nursingProjectMapper.getAllProjects();
    }

    /**
     * 获取所有护理项目VO列表
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> all() {

        //1.查询缓存
        List<NursingProjectVo> nursingProjectVos = (List<NursingProjectVo>) redisTemplate.opsForValue().get(NURSING_PROJECT_ALL);

        //2.如果缓存存在，直接将数据返回
        if (ObjectUtil.isNotEmpty(nursingProjectVos)) {
            return nursingProjectVos;
        }

        //3.如果数据不存在，则查询数据库，并写入缓存，并设置12小时过期时间
        nursingProjectVos = nursingProjectMapper.all();
        redisTemplate.opsForValue().set(NURSING_PROJECT_ALL, nursingProjectVos, 12, TimeUnit.HOURS);
        //4.返回查询结果
        return nursingProjectVos;
    }

    /**
     * 小程序分页查询护理项目
     *
     * @param ordersProjectPageParamDTO
     * @return
     */
    @Override
    public List<NursingProject> getPage(OrdersProjectPageParamDTO ordersProjectPageParamDTO) {
        //创建分页参数
        Page<NursingProject> page = new Page<>(ordersProjectPageParamDTO.getPageNum(), ordersProjectPageParamDTO.getPageSize());

        return List.of();
    }

    /**
     * 清空缓存
     */
    public void clearCache() {
        redisTemplate.delete(NURSING_PROJECT_ALL);
    }
}
